/**
 * 
 */
package com.swim.recruitment.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.swim.recruitment.entity.Position;
import com.swim.recruitment.service.PositionService;

/**
 * 岗位Controller
 * @author ljw
 * @version 2023-05-30
 */
@Controller
@RequestMapping(value = "${adminPath}/recruitment/position")
public class PositionController extends BaseController {

	@Autowired
	private PositionService positionService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Position get(String id, boolean isNewRecord) {
		return positionService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("recruitment:position:view")
	@RequestMapping(value = {"list", ""})
	public String list(Position position, Model model) {
		model.addAttribute("position", position);
		return "swim/recruitment/positionList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Position> listData(Position position, HttpServletRequest request, HttpServletResponse response) {
		Page<Position> page = positionService.findPage(new Page<Position>(request, response), position); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("recruitment:position:view")
	@RequestMapping(value = "form")
	public String form(Position position, Model model) {
		model.addAttribute("position", position);
		return "swim/recruitment/positionForm";
	}

	/**
	 * 保存岗位
	 */
	@RequiresPermissions("recruitment:position:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Position position) {
		positionService.save(position);
		return renderResult(Global.TRUE, text("保存岗位成功！"));
	}
	
	/**
	 * 删除岗位
	 */
	@RequiresPermissions("recruitment:position:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Position position) {
		positionService.delete(position);
		return renderResult(Global.TRUE, text("删除岗位成功！"));
	}
	
	/**
	 * 获取详情数据
	 */
	@RequestMapping(value = "detailsData")
	@ResponseBody
	public String detailsData(String positionId) {
		return renderResult(Global.TRUE, text("获取详情数据成功！"), positionService.get(positionId));
	}
	
	/**
	 * 查询其它职位
	 * @param excludePositionId 排除的岗位id
	 * @param natatoriumInfoId 游泳馆id
	 */
	@RequestMapping(value = "findOtherPosition")
	@ResponseBody
	public List<Position> findOtherPosition(String excludePositionId, String natatoriumInfoId) {
		List<Position> resultList = new ArrayList<Position>();
		Position positionWhere = new Position();
		positionWhere.setNatatoriumInfoId(natatoriumInfoId);
		positionWhere.setPage(new Page<>(1, 5));
		Page<Position> page = positionService.findPage(positionWhere);
		List<Position> positionList = page.getList(); 
		if(CollectionUtils.isNotEmpty(positionList)) {
			for (Position position : positionList) {
				if(!position.getId().equals(excludePositionId)) {
					resultList.add(position);
				}
			}
		}
		return resultList;
	}
	
}